package com.sgcc.fj.pmos.e_fj_mall.presenter;

import com.blankj.utilcode.util.SPUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.e_fj_mall.bean.MallGoodsBean;
import com.sgcc.fj.pmos.e_fj_mall.contract.GoodsSearchContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class GoodsSearchPresenter extends BasePresenter<GoodsSearchContract.View> implements GoodsSearchContract.Presenter {

    /**
     * 商品列表
     *
     * @param pageSize
     * @param isLoading
     */
    @Override
    public void getGoodsList(int pageSize, boolean isLoading, String searchContent) {
        Map<String, Object> params = new HashMap<>();
        params.putAll(ListPageUtil.getPageInfo(pageSize));
        params.put("combName", searchContent);
        params.put("ratioId", SPUtils.getInstance().getString("ratioId","1"));
        params.put("membersSdId", "");
        params.put("sort", "");
        params.put("soldEnergySort", "");
        params.put("avgPriceSort", "");
        params.put("createDateSort", "");
        Map<String, Object> params1 = new HashMap<>();
        params.put("packageInfoCombVo", params1);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_PACKAGE, new MvpNetCallback<MallGoodsBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsListFailed();
            }

            @Override
            public void onResponse(MallGoodsBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsListSuccess(response);
            }
        });
    }

    /**
     * 加入收藏
     *
     * @param membersUserName
     * @param membersSdId
     * @param membersSdName
     * @param combId
     * @param combName
     */
    @Override
    public void addUserPackageCollection(String membersUserName, String membersSdId, String membersSdName, String combId, String combName) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersUserName", membersUserName);
        params.put("membersSdId", membersSdId);
        params.put("membersSdName", membersSdName);
        params.put("ptype", "1");
        params.put("ustatus", "01");
        params.put("combId", combId);
        params.put("combName", combName);
        params.put("remark", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionSuccess(response);
            }
        });
    }

    /**
     * 取消收藏商品/店铺
     *
     * @param combId      套餐ID
     * @param membersSdId 店铺Id
     */
    @Override
    public void deletePackageById(String combId, String membersSdId) {
        Map<String, Object> params = new HashMap<>();
        params.put("ptype", "1");
        params.put("updatePersonId", "");
        params.put("combId", combId);
        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_DELE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionSuccess(response);
            }
        });
    }


//    @Override
//    public void onDeletePk(String guid, boolean isLoading) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("_t", System.currentTimeMillis());
//        params.put("guid", guid);
//        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_DELETE_COLLECTION, new MvpNetCallback<String>(getView(), isLoading) {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                super.onError(call, e, id);
//                // 失败回调 判断界面是否销毁
//                getView().deleteFailed();
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                super.onResponse(response, id);
//                getView().deleteSuccess();
//            }
//        });
//    }
}
